<?php
//Check for access file
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class design course
 */
class Course extends Front_Controller {

    private $ci;
    
    function  __construct() {
        parent::Front_Controller();
    }

    /**
     * Redirect to action
     */
    public function index() {
        $this->ci = & get_instance();
        $act = $this->ci->uri->segment(2);

        switch ($act) {
            case 'thiet-lap':
                $this->addCourse();
                break;
            case 'chi-tiet':
                $this->detailCourse();
                break;
            case 'cap-nhat':
                $this->updateCourse();
                break;
            case 'tim-khoa-hoc':
                $this->searchCourse();
                break;
            default:    //Special Course
                $this->specialCourse();
                break;
        }
    }

//=======================================================================//
    //                              FOR PROVIDER                             //
    //=======================================================================//
    /**
     * @desc Introduce course
     * @return void()
     */
    function addCourse() {
        if (isset($_POST['submit'])) {
            //Add subject
            if (empty($_POST['course_subject_id'])) {
                $arrInputSubject = array(
                    'subject_name' => $_POST['course_subject_name'],
                    'subject_provider_id'   => isset($_POST['provider_id'])?$_POST['provider_id']:1
                );
                $this->ci->load->module_model('subject.subjects_model');
                $_POST['course_subject_id'] = $this->ci->subjects_model->insert($arrInputSubject);
            }

            $parseDate = isset($_POST['course_khaigiang'])?explode('/', $_POST['course_khaigiang']):NULL;
            $start_date = (count($parseDate) > 1)?$parseDate[2] . '-' . $parseDate[1] . '-' . $parseDate[0]: NULL;
            $arrData = array(
                'course_provider_id' => isset($_POST['provider_id'])?$_POST['provider_id']:1,
                'course_name' => $_POST['course_name'],
                'course_field_id'   =>  $_POST['field'],
                'course_subject_id' =>  $_POST['course_subject_id'],
                'course_description' => $_POST['course_desc'],
                'create_date' => date('Y-m-d H:i:s'),
                'modified_date' => date('Y-m-d H:i:s'),
                'start_date' => $start_date,
                'end_date' => isset($_POST['course_ketthuc'])?$_POST['course_ketthuc']:NULL,
                'duration'  => $_POST['course_tiethoc'],
                'range_duration'    => $_POST['cboThoiluong'],
                'schedule'  => $_POST['course_lichhoc'],
                'max_num_learner' => $_POST['course_gioihandk'],
                'address' => $_POST['course_address'],
                'district_id'  => $_POST['district'],
                'province_id'  => isset($_POST['province_id'])?$_POST['province_id']:10,
                'max_day_regrister_offline' => $_POST['course_songaydkoffline'],
                'range_fee' => $_POST['cboHocphi'],
                'course_fee' => $_POST['course_hocphi'],
                'discount_fee' => $_POST['course_giamhocphi'],
                'entry_requirement' => $_POST['course_chungchi'],
                'other_info' => $_POST['course_thongtinkhac'],
                'teacher'   => $_POST['course_giangvien'],
                'current_status'   => 0
            );

            $this->ci->load->module_model('course.courses_model');
            $result = $this->ci->courses_model->insert($arrData);
            if ($result) {
                //Redirect self
                redirect('khoa-hoc/thiet-lap?mess=1');
            }
        }

        $this->view('course_register');
    }

//End Index
    /**
     * @desc Chi tiet khoa hoc
     */
    function detailCourse() {
        $arrParam = $this->ci->uri->segment_array();
        if (!empty($arrParam[3])) {
            $_courseId = $arrParam[3];
            $this->ci->load->module_model('course.courses_model');
            $arrData = array('course_id' => $_courseId);
            $result = $this->ci->courses_model->getCourseById($arrData);
            $this->add('results', $result);
            $this->view('course_detail');

        }
    }

    /**
     * @desc Cap nhat khoa hoc
     */
    function updateCourse() {
        $arrParam = $this->ci->uri->segment_array();
        print_r($arrParam);
        die;
    }

//=======================================================================//
    //                              FOR LEARNER                              //
    //=======================================================================//
    /**
     * @desc   Search course
     */
    function specialCourse() {
        $arrParam = $this->ci->uri->segment_array();
        print_r($arrParam);
        die;


        $this->view('course_special');
    }

    /**
     * @desc   Search course
     */
    function searchCourse() {

        //If submit
        if (isset($_POST['timkiem'])) {
            $arrParam = $_POST;
            if (!empty($arrParam)) {
                $arrParam['offset'] = 0;
                $arrParam['limit'] = 10;

                $this->ci->load->module_model('course.courses_model');
                $result = $this->ci->courses_model->getCourseBySearch($arrParam);
            }
            echo '<pre>';
            print_r($result);
            die;
        }
        //End submit

        $this->view('course_search');
    }
}